from backend.utils import Interp


Table12 = (
(17,      12,         1.7,        0.8,        3.4,         1.6  ),
(43,      30,         2,          1,          4,           2    ),
(85,      60,         2.3,        1.2,        4.6,         2.4  ),
(177,     125,        3,          1.6,        6,           3.2  ),
(354,     250,        4,          2.5,        8,           5    ),

(566,     400,        6,          3.5,        12,          7    ),
(707,     500,        8,          4.5,        16,          9    ),
(934,     660,        10.5,       6,          21,          12   ),
(1061,    750,        12,         6.5,        24,          13   ),
(1414,    1000,       16,         9,          32,          18   ),

(1768,    1250,       20,         11.4,       40,          22.8 ),
(2263,    1600,       25,         14.3,       50,          28.6 ),
(2828,    2000,       32,         18.3,       64,          36.6 ),
(3535,    2500,       40,         22.9,       80,          45.8 ),
(4525,    3200,       50,         28.6,       100,         57.2 ),

(5656,    4000,       63,         36,         126,         72   ),
(7070,    5000,       80,         45.7,       160,         91.4 ),
(8909,    6300,       100,        57.1,       200,         114.2),
(11312,   8000,       125,        71.4,       250,         142.8),
(14140,   10000,      160,        91.4,       320,         182.8),
)

def Table12_Process(
              multiple_num, 
              voltage_type, 
              peak_voltage):

    # 电压的列索引
    if voltage_type == 0:   # DC
        Vidx = 0
    else:
        Vidx = 1
    
    # 电气间隙/爬电距离的列索引
    if multiple_num == 0:   # 一重
        ECidx, CDidx = 3, 2     
    else:   # 两重
        ECidx, CDidx = 5, 4

    if peak_voltage <= Table12[0][Vidx]:
        ElecClearance, CreepDist = Table12[0][ECidx], Table12[0][CDidx]
        return ElecClearance, CreepDist
    
    for i in range(1, len(Table12)):
        if peak_voltage > Table12[i-1][Vidx] and peak_voltage <= Table12[i][Vidx]:

            CreepDist = Interp(Table12[i-1][Vidx], Table12[i][Vidx],  
                               Table12[i-1][CDidx], Table12[i][CDidx],
                               peak_voltage)
            
            if peak_voltage > 2800:
                ElecClearance = Interp(Table12[i-1][Vidx], Table12[i][Vidx],  
                               Table12[i-1][ECidx], Table12[i][ECidx],
                               peak_voltage)
            else:
                ElecClearance = Table12[i][ECidx]

            return ElecClearance, CreepDist
        
    return "不可查", "不可查"
